<?php
class Meeting{
	/**
	 * Enter description here...
	 *
	 * @var Mysql
	 */
	private $db;
	
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	public function add( array $value, $iscenter = false)
	{
		$v = array();
		$arr_login = AdminUser::getLoginInfo();
		$v['subject'] = $value['subject'];
		$v['type'] = $value['type'];
		$v['address'] = $value['address'];
		$v['content'] = $value['content'];
		$v['seq'] = $value['seq'];
		$v['pubtime'] = $value['pubtime'];
		$v['adminid'] = $arr_login['id'];
		
		$mid = $this->db->insert("meeting", $v, true);
			
		$this->updateGroupCache();
	}

	public function change($mid, array $value)
	{
		$v = array();
		isset($value['type']) && $v['type'] = $value['type'];
		isset($value['content']) && $v['content'] = $value['content'];
		isset($value['subject']) && $v['subject'] = $value['subject'];
		isset($value['address']) && $v['address'] = $value['address'];
		isset($value['seq']) && $v['seq'] = $value['seq'];
		isset($value['pubtime']) && $v['pubtime'] = $value['pubtime'];

		$this->db->update("meeting", $v, 'mid=' . $mid);
		$this->updateGroupCache();
	}
	
	public function del($mid)
	{
		$this->db->delete("meeting", 'mid=' . $mid);
		$this->updateGroupCache();
	}
	
	
	/**
	 * 读取发展论坛，会议列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function listmeeting($nowp, $per_page)
	{		
		 $totalNum = $this->db->fetchOne ('select count(*) from meeting');
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select t1.*,t2.name as typename,t3.username from meeting t1
         left join alllist t2 on t2.id=t1.type 
         left join admin t3 on t3.id=t1.adminid
         order by seq desc,mid desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	/**
	 * 读取发展论坛，会议列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function listmeetingByType($type, $nowp, $per_page)
	{
		$type = (int)$type;
		 $totalNum = $this->db->fetchOne ('select count(*) from meeting where type=' . $type);
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select * from meeting where type=' . $type . ' order by seq desc,mid desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	private function updateGroupCache()
	{
		
	}
	
	public function show($mid)
	{
		return $this->db->fetchRow("select * from meeting where mid=\"?\"", array($mid));
	}
}